﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperMarketInfo.Model;
using System.Xml.Linq;
using System.Globalization;

namespace Main
{
    class Program
    {
        static void Main(string[] args)
        {
            GetSalesReport();
        }

        public static void GetSalesReport()
        {
            using (SupermarketInformationEntities context = new SupermarketInformationEntities())
            {
                var sum = (from r in context.Reports select r.Sum).Sum();

                var result =
                    from r in context.Reports
                    join p in context.Products on r.ProductID equals p.ID
                    join v in context.Vendors on p.VendorID equals v.ID
                    group v by new {
                        v.VendorName,
                        r.ReportDate,
                        sum = (from rr in context.Reports where rr.ReportDate == r.ReportDate && rr.Product.VendorID == v.ID select rr.Sum).Sum() 
                    } into newObj
                    select new { Vendor = newObj.Key.VendorName, Date = newObj.Key.ReportDate, Sum = newObj.Key.sum };

                Dictionary<string, List<KeyValuePair<DateTime, decimal>>> expenses =
                    new Dictionary<string, List<KeyValuePair<DateTime, decimal>>>();

                foreach (var item in result)
                {
                    if (expenses.ContainsKey(item.Vendor))
                    {
                        expenses[item.Vendor].Add(new KeyValuePair<DateTime, decimal>(item.Date, item.Sum));
                    }
                    else
                    {
                        expenses.Add(item.Vendor, new List<KeyValuePair<DateTime, decimal>>() { new KeyValuePair<DateTime, decimal>(item.Date, item.Sum) });
                    }
                }

                SaveXMLToFile(expenses);
            }
        }

        public static void SaveXMLToFile(Dictionary<string, List<KeyValuePair<DateTime, decimal>>> salesReport)
        {
            XElement rootElement = new XElement("sales");

            foreach (var currentSaleReport in salesReport)
            {
                XElement currentVendor = new XElement("sale", new XAttribute("vendor", currentSaleReport.Key));

                Dictionary<string, decimal> zz = new Dictionary<string, decimal>();
                foreach (var item in currentSaleReport.Value)
                {
                    string qqq = item.Key.ToString("MMM-yyyy", CultureInfo.InvariantCulture);
                    if (zz.ContainsKey(qqq))
                    {
                        zz[qqq] += item.Value;
                    }
                    else
                    {
                        zz.Add(qqq, item.Value);
                    }
                }

                foreach (var item in zz)
                {
                    XElement currentVendorSummary = new XElement("expenses",

                        new XAttribute("month", String.Format("{0}", item.Key)));
                    currentVendorSummary.Add(string.Format("{0:C2}", item.Value.ToString(CultureInfo.InvariantCulture)));

                    currentVendor.Add(currentVendorSummary);
                }

                rootElement.Add(currentVendor);
            }

            Console.WriteLine(rootElement);
            rootElement.Save("../../sales-report.xml");

            Console.WriteLine("You successfully created file.");
        }
    }
}
